package com.ydgg.ltar.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.ydgg.ltar.mapper.AreaGiftMapper;
import com.ydgg.ltar.model.AreaGift;
import com.ydgg.ltar.model.AreaGiftExample;
import com.ydgg.ltar.util.PageInfo;
import com.ydgg.ltar.util.Result;

/**
 * 礼物区域
 * 作者： 张叶
 * 创建时间：2019年3月18日  下午2:35:27
 */
@Service
public class AreaGiftService {

	@Autowired
	private AreaGiftMapper areaGiftMapper;
	
	@Autowired
	private QiniuService qiniuService;

	
	/**
	 * 列表
	 * 作者： 张叶
	 * 创建时间：2019年3月14日  上午10:54:45
	 */
	public Map<String, Object> selectByPage(PageInfo pageInfo,Integer rows, Integer page,
			String sidx, String sord, String name) {
		
		if (pageInfo == null) {
			pageInfo = new PageInfo();
		}
		if (pageInfo.getPage() < 2
				|| pageInfo.getPage() >= pageInfo.getTotalPage()) {
			pageInfo.setCount(0);
		}
		if (rows != null) {// 每页显示条数
			pageInfo.setPageSize(rows);
		}
		if (page != null) {// 当前页
			pageInfo.setPage(page);
		}

		// 排序
		String order = " order by ag.created_at desc";
		if (sidx != null && sidx != "") {// 排序字段
			order = " order by " + sidx + " ";
			if (sord != null && sord != "") {// 排序
				order += sord;
			}
		}
		String where = "1 = 1 and ag.status != 'deleted'";

		if(name != null && !"".equals(name)){
			where += " and ag.name like '%" + name + "%'";
		}

		Map<String, Object> param = new HashMap<String, Object>();
		param.put("myCount", pageInfo.getCount());
		param.put("pageSize", pageInfo.getPageSize());
		param.put("page", pageInfo.getPage());
		param.put("where", where);
		param.put("order", order);
		List<Map<String, Object>> m = areaGiftMapper.selectByPage(param);
		pageInfo.setCount(Integer.parseInt(param.get("myCount").toString()));
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for(int i = 0 ; i < m.size() ; i ++){
			Date createTime = (Date) m.get(i).get("created_at");
			m.get(i).put("created_at", sdf.format(createTime));
		}
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("total", pageInfo.getTotalPage());
		data.put("list", m);
		return data;
	}
	
	
	public Result insert(AreaGift ag,MultipartFile imageUrls){
		Result result = new Result();
		
		String imageUrl = "";
		try {
			imageUrl = qiniuService.upload("file", imageUrls.getOriginalFilename(),imageUrls.getBytes());
		} catch (Exception e) {
			result.code = 2;
			result.message = "图片上传失败，添加失败";
		}
		ag.setCreatedAt(new Date());
		ag.setStatus("on");
		ag.setImageUrl(imageUrl);
		int i = areaGiftMapper.insert(ag);
		if(i > 0){
			result.code = 0;
			result.message = "添加成功";
		}else{
			result.code = 1;
			result.message = "系统错误，添加失败";
		}
		return result;
	}

	
	/**
	 * 删除
	 * 作者： 张叶
	 * 创建时间：2019年3月14日  上午10:53:26
	 */
	@Transactional
	public Result deleted(Long id){
		Result result = new Result();
		AreaGift ag = areaGiftMapper.selectByPrimaryKey(id);
		if(ag != null){
			ag.setStatus("deleted");
			int i = areaGiftMapper.updateByPrimaryKeySelective(ag);
			if(i > 0){
				result.code = 0;
				result.message = "添加成功";
			}else{
				result.code = 1;
				result.message = "系统错误，添加失败";
			}
		}else{
			result.code = 0;
			result.message = "删除成功";
		}
		return result;
	}

	
	public AreaGift selectByPrimaryKey(Long id){
		AreaGift a = areaGiftMapper.selectByPrimaryKey(id);
		return a;
	}

	public Result updateBySelective(AreaGift ag,MultipartFile imageUrls){
		Result result = new Result();
		try {
			if(imageUrls.getBytes().length > 0){
				String imageUrl = qiniuService.upload("file", imageUrls.getOriginalFilename(),imageUrls.getBytes());
				ag.setImageUrl(imageUrl);
			}
			int i = areaGiftMapper.updateByPrimaryKeySelective(ag);
			if( i > 0){
				result.code = 0;
				result.message = "修改成功";
			}else{
				result.code = 1;
				result.message = "系统错误，修改失败";
			}
		} catch (Exception e) {
			result.code = 2;
			result.message = "图片上传失败，添加失败";
		}
		return result;
	}
	
	
	public List<AreaGift> selectByExample(AreaGiftExample example){
		List<AreaGift> list = areaGiftMapper.selectByExample(example);
		return list;
	}
}
